package m.store.service;

import m.base.model.StoreInfo;
import m.base.model.UserInfo;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryCheck;
import m.common.model.util.ModelUpdateUtil;
import m.common.service.Service;
import m.store.model.GuestInfo;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.ObjectUtil;

public class GuestInfoService extends Service {
    public String save(GuestInfo model) throws Exception {
        String msg;
        ModelQueryCheck.init(model)
            .checkNotNull("name","客户名称不能为空")
            .checkNotNull("store.oid","登录用户商户不存在")
            .doCheck();
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"name","store.oid"},"名称已存在");
        if(ObjectUtil.isEmptyOid(model)){
            model.setOid(GenerateID.generatePrimaryKey());
            ModelUpdateUtil.insertModel(model);
            msg="保存成功";
        }else{
            ModelUpdateUtil.updateModel(model,"manageUser.oid","dutyUser","name","type","tel","address","description");
            msg="修改成功";
        }
        return msg;
    }
    public void addSelf(StoreInfo store, UserInfo user) throws MException {
        GuestInfo model=new GuestInfo();
        model.setOid(store.getOid());
        model.setInputUser(user);
        model.setManageUser(user);
        model.setStore(store);
        model.setName(store.getName());
        model.setType(GuestInfo.Type_Company);
        model.setDutyUser(user.getRealname());
        model.setTel(store.getTel());
        model.setAddress(store.getAddress());
        model.setDescription("生成自家公司，用于期初入库和生产入库。");
        ModelUpdateUtil.insertModel(model);
    }
}
